<?php 

// namespace Phpcode\Sql; 命名空间

header("Content-Type:text/html;charset=utf-8");

// 链接数据库 加@符号抑制非致命错误 mysql对象$connect
$connect =  @mysqli_connect("localhost","root","root");
// 设置端口 3306 
// 链接时提示错误信息
if (!$connect) {
    echo"【链接数据库失败!】:".mysqli_connect_error();
    // 链接时提示错误码
    echo"【链接时提示错误码!】:".mysqli_connect_errno();
    exit();
}
// mysqli_set_charset 	设置编码
$charset = mysqli_set_charset($connect,'UTF8');
if(!$charset) {
    echo"【编码不对!】:".mysqli_error($connect);
    exit();
}
// 选择数据库
$select =  mysqli_select_db($connect, "company");

if (!$select) {
    echo"【数据库不对!】:".mysqli_error($connect);
    exit();
}
// var_dump($select);
// exit;
// 设置表前缀 

//添加
function insert(){
    // 引入全局变量
    global $connect;
    global $pre_;
    // 书写sql语句 
    $sql = "INSERT INTO {$pre_}person (`nickname`,`mobile`,`depid`,`jobid`) VALUES ('小黄','12903245','2','2')";
    // 执行这个语句返回一个字符串
    $res =  @mysqli_query($connect,$sql);
    // 判断查询语句是否正确
    if(!$res){
        echo "【sql执行错误】:".mysqli_error($connect);
        exit;
    }
    // 获取自增主键 返回最后一个查询中自动生成的 ID 
       
    return mysqli_insert_id($connect);

}
// $insert =  insert();
// echo $insert;

//修改
function update(){
    global $connect;
    global $pre_;
    $sql = "UPDATE {$pre_}person SET `nickname` = '神刀夜狗' WHERE id > 34";
    $res = mysqli_query($connect,$sql);
    if(!$res){
        echo "【sql执行错误】:".mysqli_error($connect);
        exit;
    }
    //返回操作成功的行数
    $rows = mysqli_affected_rows($connect);
    return $rows;
}
// $rows = update();
// echo $rows;
//删除
function delete(){
    global $connect;
    global $pre_;
    $sql = "DELETE FROM {$pre_}person WHERE `nickname` = '神刀夜狗'";
    $res = mysqli_query($connect,$sql);

    if(!$res){
        echo "【sql执行错误】:".mysqli_error($connect);
        exit;
    }
    return mysqli_affected_rows($connect);
}
// $rows = delete();
// echo $rows;


// localhost
function select(){
    global $connect;
    global $pre_;
    //单查询
    // $sql = "SELECT * FROM {$pre_}person";
    //左连接查询 left join 右外连接  {就改成 right 就行 inner 内连接}不常用
    $sql = "SELECT person.*,dept.name AS deptname,job.name AS jobname FROM {$pre_}person AS person LEFT JOIN {$pre_}department AS dept ON dept.id = person.depid LEFT JOIN {$pre_}job AS job ON job.id = person.jobid";
    // $sql = "SELECT person.*,dept.name AS deptname,job.name AS jobname FROM {$pre_}person AS person Right JOIN {$pre_}department AS dept ON dept.id = person.depid LEFT JOIN {$pre_}job AS job ON job.id = person.jobid";
    // $sql = "SELECT person.*,dept.name AS deptname,job.name AS jobname FROM {$pre_}person AS person inner JOIN {$pre_}department AS dept ON dept.id = person.depid LEFT JOIN {$pre_}job AS job ON job.id = person.jobid";
   //模糊查询 % 匹配任意字符 任意长度 ORDER BY 排序 ASE升序 DESC 降序 limit 分页 两个参数 第一个是起始位置 第二个是长度；
    // $sql = "SELECT id,nickname FROM {$pre_}person WHERE nickname LIKE '%张三%' ORDER BY id DESC LIMIT 0,4";
    $res = mysqli_query($connect,$sql);
    if(!$res){
        echo "【sql执行错误】:".mysqli_error($connect);
        exit;
    }
    //返回关联数组对象
    $data = [];
    //使用while循环来拿数据  $rows = mysqli_fetch_assoc( $res) 每次拿一挑数据 直到拿到最后一条数据停
    while($rows = mysqli_fetch_assoc( $res))
    {
        $data[] = $rows;
    }
    return $data;

}
$arr = select();
var_dump($arr) ;


?>